package cn.mpq.payment.controller;

import cn.mpq.payment.common.Result;
import cn.mpq.payment.common.WXAppletUserInfo;
import cn.mpq.payment.model.User;
import cn.mpq.payment.service.UserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/user")
public class WXAppController {
    @Autowired
    private UserService userService;
    // 获取微信用户信息openid
    @RequestMapping(value = "getWxUser")
    public Result getWxUser(String code) throws Exception {
       JSONObject jsonObject = WXAppletUserInfo.getSessionKeyOropenid(code);
       String openId = jsonObject.getString("openid");
       String sessionKey = jsonObject.getString("session_key");
       User user = userService.selectOne(new EntityWrapper<User>().eq("openid",openId));
       if (user==null){
           User user1 = new User();
           user1.setOpenid(openId);
           user1.setSessionkey(sessionKey);
           userService.insert(user1);
       }else {
           user.setUpdateTime(new Date());
           user.setOpenid(openId);
           user.setSessionkey(sessionKey);
           userService.update(user,new EntityWrapper<User>().eq("openid",openId));
       }
       return new Result(0,"获取成功",openId);
    }
    // 更新用户信息
    @RequestMapping(value = "updateWxUserInfo")
    public Result updateWxUserInfo(String encryptedData,String openid,String iv) throws Exception {
        User user = userService.selectOne(new EntityWrapper<User>().eq("openid",openid));
        JSONObject jsonObject = WXAppletUserInfo.getUserInfo(encryptedData,user.getSessionkey(),iv);
        user.setCity(jsonObject.getString("city"));
        user.setCountry(jsonObject.getString("country"));
        user.setSex(Integer.valueOf(jsonObject.getString("gender")));
        user.setHeadimgurl(jsonObject.getString("avatarUrl"));
        user.setLanguage(jsonObject.getString("language"));
        user.setNickname(jsonObject.getString("nickName"));
        user.setUpdateTime(new Date());
        userService.update(user,new EntityWrapper<User>().eq("openid",openid));
        User user1 = userService.selectOne(new EntityWrapper<User>().eq("openid",openid));
        Integer code = 1;
        if (user1!=null){
            String name = user1.getName();
            if (StringUtils.isNotBlank(name)){
                code = 0;
            }
        }
        return new Result(code,"获取成功");
    }
    // 更新用户信息
    @RequestMapping(value = "updateWxUser")
    public Result updateWxUser(String model,String openid,String system) throws Exception {
        User user = new User();
        user.setModel(model);
        user.setUpdateTime(new Date());
        user.setSystem(system);
        userService.update(user,new EntityWrapper<User>().eq("openid",openid));
        return new Result(0,"获取成功");
    }
    // 获取用户信息
    @RequestMapping(value = "getUserInfo")
    public Result getUserInfo(String openid) throws Exception {
        JSONObject jsonObject = WXAppletUserInfo.getaccessToken();
        String accessToken = jsonObject.getString("access_token");
        JSONObject jsonObjects = WXAppletUserInfo.getUserInfo(openid,accessToken);
        return new Result(0,"获取成功",jsonObjects);
    }
}
